<template>
  <div id="login-container">
    <div class="main">
      <div class="content">
        <h1>易查企app后台管理系统</h1>
        <el-form :model="form" label-width="auto">
          <el-form-item label="用户名">
            <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              v-model="form.password"
              show-password
              type="password"
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="onSubmit">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive } from 'vue'
import { login } from '@/api/user'
import { ElMessage } from 'element-plus'
const form = reactive({
  username: '',
  password: '',
})

const onSubmit = async () => {
  const res = await login(form.username, form.password)
  console.log(res)

  if (res.status === 201 || res.status === 200) {
    ElMessage.success('登录成功!')
    localStorage.setItem('access_token', res.data.data.accessToken)
    localStorage.setItem('refresh_token', res.data.data.refreshToken)
    localStorage.setItem('user_info', JSON.stringify(res.data.data.userInfo))
    setInterval(() => {
      window.location.href = '/'
    }, 1000)
  } else {
    ElMessage.error(res.data.data || '系统繁忙，请稍后再试')
  }
}
</script>

<style scoped lang="scss">
#login-container {
  background-image: url('../../assets/image.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  z-index: -1;
  .main {
    width: 400px;
    height: 100vh;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    right: 50px;
    padding: 0 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .content {
      width: 100%;
      h1 {
        font-size: 30px;
        font-weight: bold;
        margin-bottom: 20px;
        text-align: center;
      }
      .flex-between {
        width: 100%;
        display: flex;
        justify-content: end;
      }
    }
  }
}
</style>
